﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
//using LPV_POS.In;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang8_Tinh_Tien_HOTELTHANG : DevExpress.XtraEditors.XtraForm
    {
        #region "   Khai bao DataTable"
        DataTable tbBH_PHIEU_TMP = null;
        DataTable tbBH_PHIEU_CT_TMP = null;
        //DataTable tbDonGia = null;
        #endregion

        #region "   mh_banhang8_Tinh_Tien_HOTELTHANG"
        public mh_banhang8_Tinh_Tien_HOTELTHANG(DataTable _tbBH_PHIEU_TMP, DataTable _tbBH_PHIEU_CT_TMP)
        {
            InitializeComponent();
            tbBH_PHIEU_TMP = _tbBH_PHIEU_TMP;
            tbBH_PHIEU_CT_TMP = _tbBH_PHIEU_CT_TMP;

            this.SetgrdDonGia_DataSource();
            txtGhiChu.Text = _tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] + "";

            #region "   Load Don Gia"
            thuvien othuvien = new thuvien();
            DataTable tbDonGia = othuvien.SP_SelectDataTable("sp_DM_TIENGIO4",
                                                         new string[] { "@flag", "@DM_BAN_MA" },
                                                         new object[] { 7, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"] });
            if (tbDonGia.Rows.Count > 0)
            {
                decimal dgia = 0;
                decimal.TryParse(tbDonGia.Rows[0]["T2"] + "", out dgia);
                if (tbDonGia.Rows[0]["TINH_GIA_USD"] + "" == "True")
                {
                    int iTI_GIA_USD = 0;
                    int.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["TI_GIA_USD"] + "", out iTI_GIA_USD);
                    dgia = dgia * iTI_GIA_USD;
                }
                if (dgia > 1000000000)
                {
                    MessageBox_LPV.Show("cấu hình tiền phòng quá lớn\nVui lòng xem lại phần danh mục tiền giờ");
                    return;
                }
                else
                    nudDonGia.Value = dgia;
                nudDonGia.Tag = tbDonGia.Rows[0]["OBJID"];

                //DataTable tbDM_TIENGIO4_THEM_GIO = othuvien.SP_SelectDataTable("sp_DM_TIENGIO4_THEM_GIO",
                //                                    new string[] { "@flag", "@DM_TIENGIO4_OBJID" },
                //                                    new object[] { 4, tbDonGia.Rows[0]["OBJID"] });
                //grdDM_TIENGIO4_THEM_GIO.DataSource = tbDM_TIENGIO4_THEM_GIO;
            }

            #endregion

        }
        #endregion

        #region "   SetgrdDonGia_DataSource"
        void SetgrdDonGia_DataSource()
        {
            try
            {
                DataTable tbDG = tbBH_PHIEU_CT_TMP.Copy();
                for (int i = 0; i < tbDG.Rows.Count; i++)
                {
                    if (string.Concat( tbDG.Rows[i]["MAHH"] ,"").IndexOf("Tien_Gio")<0)
                    {
                        tbDG.Rows.RemoveAt(i);
                        i--;
                    }
                }
                for (int i = 0; i < tbDG.Rows.Count; i++)
                    tbDG.Rows[i]["STT"] = i + 1;
                grdTienPhong.DataSource = tbDG;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region "   mh_banhang8_Tinh_Tien_HOTELTHANG_Load"
        private void mh_banhang8_Tinh_Tien_HOTELTHANG_Load(object sender, EventArgs e)
        {
            this.ShowInfo();
        }
        #endregion

        #region "   ShowForm()"
        float fso = 0;
        public float ShowForm()
        {
            this.ShowDialog();
            return fso;
        }
        #endregion

        #region "   ShowInfo()"
        void ShowInfo()
        {
            try
            {


                DateTime dNGAY_GIO_VAO;
                if (tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "" == "")
                    tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = Variable.ngay_gio_hienhanh;

                DateTime.TryParse(tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "", out dNGAY_GIO_VAO);
                dtpVao.Value = dNGAY_GIO_VAO;

                //
                dtpRa.Value = Variable.ngay_gio_hienhanh;

            }
            catch (Exception ex)
            {
                string sx = ex.Message;
            }
        }
        #endregion

        #region "   btnghichu_Click"
        private void btnghichu_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string str = omh_chu.ShowForm("Ghi chú", txtGhiChu.Text, 300, 300, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtGhiChu.Text = str;
        }
        #endregion

        private void btnTinhTienGio_Click(object sender, EventArgs e)
        {
            #region "   ko xai"
            //string sT = "";
            //if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Monday)
            //    sT = "T2";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Tuesday)
            //    sT = "T3";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Wednesday)
            //    sT = "T4";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Thursday)
            //    sT = "T5";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Friday)
            //    sT = "T6";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Saturday)
            //    sT = "T7";
            //else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Sunday)
            //    sT = "T1";
            //lblNgay_Gio_Vao.Text = lblNgay_Gio_Vao.Tag + " (" + sT.Replace("T1", "CN") + ")";
            #endregion

            thuvien othuvien = new thuvien();
            DataTable tbTinhTienGio = othuvien.SP_SelectDataTable("sp_TinhTienGio",
                                                            new string[] { "@flag", "@gio_vao", "@gio_ra", "@tienDM", "@DM_TIENGIO4_OBJID" },
                                                            new object[] { 1, dtpVao.Value, dtpRa.Value, nudDonGia.Value, nudDonGia.Tag });
            if (tbTinhTienGio.Rows.Count > 0)
            {
                DataTable tb = (DataTable)grdTienPhong.DataSource;
                for (int i = 0; i < tbTinhTienGio.Rows.Count; i++)
                {
                    if (tb.Rows.Count < i + 1)
                    {
                        DataRow dr = tb.NewRow();
                        tb.Rows.Add(dr);
                    }

                    tb.Rows[i]["SO_LUONG"] = tbTinhTienGio.Rows[i]["SO_LUONG"];
                    tb.Rows[i]["DON_GIA"] = tbTinhTienGio.Rows[i]["DON_GIA"];
                    if (tbTinhTienGio.Rows[i]["ca_ngay"] + "" == "True")
                    {
                        tb.Rows[i]["GHI_CHU"] = "";
                        tb.Rows[i]["MAHH"] = "Tien_Gio";
                        tb.Rows[i]["OBJNAME_HH_ROOT"] = "Tiền giờ";
                        tb.Rows[i]["OBJNAME_HH"] = "Tiền giờ";
                    }
                    else
                    {
                        tb.Rows[i]["GHI_CHU"] = tbTinhTienGio.Rows[i]["GHI_CHU"];
                        tb.Rows[i]["MAHH"] = "Tien_Gio_Them";
                        tb.Rows[i]["OBJNAME_HH_ROOT"] = "Tiền giờ thêm";
                        tb.Rows[i]["OBJNAME_HH"] = "Tiền giờ thêm";
                    }
                }

                for (int i = tbTinhTienGio.Rows.Count; i < tb.Rows.Count; i++)
                    tb.Rows.RemoveAt(i--);
            }
            grdTienPhong.Refresh();
        }


        #region "   btnThoat_Click"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
        #endregion

        #region "   btnThucHien_Click"
        private void btnThucHien_Click(object sender, EventArgs e)
        {//Luu Update
            //BH_PHIEU_TMP
            tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] = txtGhiChu.Text.Trim();
            tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_RA"] = dtpRa.Value;
            tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = dtpVao.Value;

            //BH_PHIEU_CT_TMP
            DataTable tb = (DataTable)grdTienPhong.DataSource;

            #region "   UPDATE lại dữ liệu TIỀN GIỜ vào tbBH_PHIEU_CT_TMP"
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length < i + 1)
                    {
                        DataRow dr = null;
                        dr = tbBH_PHIEU_CT_TMP.NewRow();
                        dr["MAHH"] = "Tien_Gio";
                        dr["CHIETKHAU_TIEN"] = 0;
                        dr["KM_TIEN"] = 0;
                        dr["VAT_TIEN"] = 0;
                        dr["GHI_CHU"] = "";
                        tbBH_PHIEU_CT_TMP.Rows.Add(dr);
                    }
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["MAHH"] = tb.Rows[i]["MAHH"];
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH_ROOT"] = tb.Rows[i]["OBJNAME_HH_ROOT"];
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH"] = tb.Rows[i]["OBJNAME_HH"];
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["SO_LUONG"] = tb.Rows[i]["SO_LUONG"];
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["DON_GIA"] = tb.Rows[i]["DON_GIA"];
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["NGAY_TINH_GIO"] = dtpRa.Value;
                    tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["GHI_CHU"] = tb.Rows[i]["GHI_CHU"];
                }
            }
            #endregion

            #region "   TAM THOI ko xai"
            //DataRow dr = null;
            //if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length > 0)
            //    dr = tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[0];
            //else
            //{
            //    dr = tbBH_PHIEU_CT_TMP.NewRow();
            //    dr["MAHH"] = "Tien_Gio";
            //    tbBH_PHIEU_CT_TMP.Rows.Add(dr);
            //    dr["CHIETKHAU_TIEN"] = 0;
            //    dr["KM_TIEN"] = 0;
            //    dr["VAT_TIEN"] = 0;
            //    dr["GHI_CHU"] = "";
            //}
            //dr["OBJNAME_HH_ROOT"] = "Tiền giờ";
            //dr["OBJNAME_HH"] = "Tiền giờ";
            //dr["SO_LUONG"] = nudTong_Gio.Value;
            //dr["DON_GIA"] = nudDonGia.Value;
            //dr["NGAY_TINH_GIO"] = dtpRa.Value;
            //dr["THANH_TIEN"] = nudThanhTien.Value;
            #endregion

            #region "   Xoa nhung dong ko nam trong TIEN PHONG CHI TIET"
            for (int i = tb.Rows.Count; i < tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length; i++)
            {
                Int64 iobjid = 0;
                Int64.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJID"] + "", out iobjid);
                if (iobjid > 0)
                {
                    string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                    int irt = new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                }
                tbBH_PHIEU_CT_TMP.Rows.Remove(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]);
                i--;
                
            }
            #endregion

        }
        #endregion
    }
}